<?php
/*
	会员管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}else{
		$params['mid >']=0;
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('member',$parameter,$total);
	if($list){
		$setting=$this->plugin_setting('setting');
		foreach($list as $k=>&$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['shmember']=cfc('core')->member_show($item['fromuser']);
			$item['myji1']=$this->count('myji',array('status'=>1,'merchid'=>$_SESSION['onemerchid'],'mid'=>intval($item['mid'])));
			$item['myji2']=$this->count('myji',array('status'=>2,'merchid'=>$_SESSION['onemerchid'],'mid'=>intval($item['mid'])));
			$item['myji3']=$this->count('myji',array('status'=>3,'merchid'=>$_SESSION['onemerchid'],'mid'=>intval($item['mid'])));
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','key'=>'mid','edit'=>false,'delall'=>false,'list'=>array(
			array('name'=>'mid','title'=>'ID','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'shmember','title'=>'上级信息','type'=>'member'),
			array('name'=>'myji1','title'=>'健康蛋鸡数','type'=>'string'),
			array('name'=>'myji2','title'=>'饥饿蛋鸡数','type'=>'string'),
			array('name'=>'myji3','title'=>'已死亡蛋鸡数','type'=>'string'),
			array('name'=>'lsfee','title'=>'粮食数量','type'=>'string'),
			array('name'=>'jd','title'=>'鸡蛋数量','type'=>'string'),
			array('name'=>'cv','title'=>'CV值','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','op'=>'change','title'=>'修改推荐人'),
			array('type'=>'modal','op'=>'setfee','title'=>'设置CV/粮食'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setfee'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$update=array(
			'lsfee'=>$post['lsfee'],
			'jd'=>$post['jd'],
			'cv'=>$post['cv'],
		);
		$this->update('member',$update,array('merchid'=>$_SESSION['onemerchid'],'mid'=>intval($post['mid'])));
		exi('操作成功！','','list');
	}
	$item=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>intval($_RQ['mid'])));
	if(!$item){
		exi('指定会员不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改推荐人',
		'edit'=>array(
			array('name'=>'lsfee','title'=>'粮食数量','type'=>'string'),
			array('name'=>'jd','title'=>'鸡蛋数量','type'=>'string'),
			array('name'=>'cv','title'=>'CV值','type'=>'string'),
		),
	);
	exi($return);
}
if($_RQ['op']=='change'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$mid=intval($post['mid']);
		$fromuser=intval($post['fromuser']);
		$this->model('member')->set_parent($mid,$fromuser,true);
		exi('操作成功！','','list');
	}
	$item=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>intval($_RQ['mid'])),array('mid','fromuser'));
	if(!$item){
		exi('指定会员不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'编辑数据',
		'edit'=>array(
			array('type'=>'choose','name'=>'fromuser','title'=>'设置推荐人','action'=>'core/member.get_member/','help'=>'填写0表示设置为总店'),
		),
	);
	exi($return);
}
if($_RQ['op']=='del'){
	$mid=intval($_RQ['mid']);
	if($mid){
		$this->delete('member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $mid){
			$mid=intval($mid);
			if($mid){
				$this->delete('member',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
			}
		}
	}
	exi('删除成功！');
}
?>